﻿using AbpDemo.AdminRoles;
using AbpDemo.Admins;
using AbpDemo.Authors;
using AbpDemo.Menus;
using AbpDemo.RoleMenus;
using AbpDemo.Roles;
using IdentityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace AbpDemo.EntityFrameworkCore;

[ConnectionStringName("Default")]
public class AbpDemoDbContext :
    AbpDbContext<AbpDemoDbContext>
{
    private readonly IHttpContextAccessor httpContext;

    /* Add DbSet properties for your Aggregate Roots / Entities here. */
    public DbSet<Admin> Admins { get; set; }
    public DbSet<Menu> Menus { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<AdminRole> AdminRoles { get; set; }
    public DbSet<RoleMenu> RoleMenus { get; set; }

    public DbSet<Author> Authors { get; set; }

    public AbpDemoDbContext(DbContextOptions<AbpDemoDbContext> options, IHttpContextAccessor httpContext)
        : base(options)
    {
        this.httpContext = httpContext;
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();

        /* Configure your own tables/entities inside here */

        builder.Entity<Admin>(b =>
        {
            b.ToTable(AbpDemoConsts.DbTablePrefix + nameof(Admin), AbpDemoConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props

            b.Property(m => m.UserName).IsRequired().HasMaxLength(50);
            b.Property(m => m.Password).IsRequired().HasMaxLength(50);
        });

        builder.Entity<Role>(b =>
        {
            b.ToTable(AbpDemoConsts.DbTablePrefix + nameof(Role), AbpDemoConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props

            b.Property(m => m.RoleName).IsRequired().HasMaxLength(50);
            b.Property(m => m.Remark).HasMaxLength(500);
        });

        builder.Entity<Menu>(b =>
        {
            b.ToTable(AbpDemoConsts.DbTablePrefix + nameof(Menu), AbpDemoConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props

            b.Property(m => m.ApiPath).IsRequired().HasMaxLength(50);
            b.Property(m => m.ComponentPath).IsRequired().HasMaxLength(50);
            b.Property(m => m.Icon).HasMaxLength(50);
            b.Property(m => m.MenuName).IsRequired().HasMaxLength(50);
            b.Property(m => m.Redirect).HasMaxLength(100);
            b.Property(m => m.RouteName).IsRequired().HasMaxLength(50);
            b.Property(m => m.RoutePath).HasMaxLength(50);
        });

        builder.Entity<AdminRole>(b =>
        {
            b.ToTable(AbpDemoConsts.DbTablePrefix + nameof(AdminRole), AbpDemoConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props

            b.HasOne<Admin>().WithMany().HasForeignKey(m => m.AdminId);
            b.HasOne<Role>().WithMany().HasForeignKey(m => m.RoleId);
        });

        builder.Entity<RoleMenu>(b =>
        {
            b.ToTable(AbpDemoConsts.DbTablePrefix + nameof(RoleMenu), AbpDemoConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props

            b.HasOne<Menu>().WithMany().HasForeignKey(m => m.MenuId);
            b.HasOne<Role>().WithMany().HasForeignKey(m => m.RoleId);
        });

        builder.Entity<Author>(b =>
        {
            b.ToTable(AbpDemoConsts.DbTablePrefix + nameof(Author), AbpDemoConsts.DbSchema);
            b.ConfigureByConvention(); //auto configure for the base class props

            b.Property(m => m.AuthorName).IsRequired().HasMaxLength(50);
            b.Property(m => m.Remark).HasMaxLength(500);
        });
    }

    public override void Initialize(AbpEfCoreDbContextInitializationContext initializationContext)
    {
        ChangeTracker.StateChanged += ChangeTracker_StateChanged1;
        ChangeTracker.Tracked += ChangeTracker_Tracked1;
        base.Initialize(initializationContext);
    }

    private void ChangeTracker_Tracked1(object sender, Microsoft.EntityFrameworkCore.ChangeTracking.EntityTrackedEventArgs e)
    {
        /*var AdminId = httpContext.HttpContext.User.Claims.First(m => m.Type == JwtClaimTypes.Id).Value;
        Admin admin = e.Entry.Entity as Admin;
        admin.CreatorId = Guid.Parse(AdminId);
        admin.LastModificationTime = DateTime.UtcNow;*/
    }

    private void ChangeTracker_StateChanged1(object sender, Microsoft.EntityFrameworkCore.ChangeTracking.EntityStateChangedEventArgs e)
    {
        
    }
}
